@ECHO OFF
rem /**
rem   * fluid - portable application framework
rem   * Copyright (c) 2012-2013, Vikas Burman, all rights reserved.
rem   * 
rem   * Licensed under the terms of the MIT license. You are free to use 'fluid' under any open source or 
rem   * commercial project, as long as this copyright header is left intact.
rem   * 
rem   * An explicit credit notice by using tiny "crafted on fluid" logo, which links back to the commercial site
rem   * of the project, is desired though not bounded by licensing terms.
rem   * 
rem   *     Project: http://fluidjs.codeplex.com/
rem   *     License: http://fluidjs.codeplex.com/license
rem   *        Blog: http://fluidjs.vikasburman.com
rem   * 
rem   *  File: fluid build script
rem   *  Dependencies: 
rem   * 	- none
rem   *  Usage:
rem   * 	build    -- opens a menu to choose from
rem	  * 	build /A -- create both debug and release builds as per configuration
rem	  * 	build /D -- create only debug build as per configuration
rem	  * 	build /R -- create only release build as per configuration
rem   *  Notes:
rem	  * 	- a root folder should contain "builds" and "source" folders
rem	  *		- build.bat must be kept at "source" root folder
rem	  * 	- build.bat must be executed with "source" being the current directory
rem	  * 	  one approach to run it from a single line is:
rem	  * 	  CMD /C {drive:} && CD "{drive:}\...\{root}\source\" && build.bat [/A | /D | /R]
rem	  *   	- "builds" folder must not have any custom files - as it gets cleaned up during build process
rem	  * 	- developers MUST do one-time build related setting for variables in "buildOptions.bat" file
rem   * 
rem   */

rem - set local variables scope
SETLOCAL

rem - define build options
IF NOT EXIST ".\customizations\buildOptions.bat" GOTO SETUP_ENV
set BUILD_OPTIONS=
CALL ".\customizations\buildOptions.bat"

:VARS
rem - save current directory (MUST be source root)
rem - that's why it is important to have build to execute from source root
pushd .

rem - change to relative directory and save value of CD (current directory) variable
cd %ROOT%
set ABSOLUTE_PATH=%CD%
set BUILD_PATH=%CD%\builds\

set TEMP_ROOT_PATH=%BUILD_PATH%temp\
set TEMP_DEBUG_PATH=%TEMP_ROOT_PATH%debug\
set TEMP_RELEASE_PATH=%TEMP_ROOT_PATH%release\

set PORTABLE_APP_DEBUG_PATH=%BUILD_PATH%portableApp\debug\
set PORTABLE_APP_RELEASE_PATH=%BUILD_PATH%portableApp\release\

set ONLINE_APP_DEBUG_PATH=%BUILD_PATH%onlineApp\debug\
set ONLINE_APP_RELEASE_PATH=%BUILD_PATH%onlineApp\release\

set OFFLINE_APP_DEBUG_PATH=%BUILD_PATH%offlineApp\debug\
set OFFLINE_APP_RELEASE_PATH=%BUILD_PATH%offlineApp\release\

set CHROME_HOSTED_APP_DEBUG_PATH=%BUILD_PATH%cwsHostedApp\debug\
set CHROME_HOSTED_APP_RELEASE_PATH=%BUILD_PATH%cwsHostedApp\release\

set WRAPPED_APP_DEBUG_PATH=%BUILD_PATH%wrappedApp\debug\
set WRAPPED_APP_DEBUG_PATH=%BUILD_PATH%wrappedApp\release\

rem - restore current directory
popd

set SOURCE_PATH=%~dp0
set BUILD_LOG=%SOURCE_PATH%build.log
set BUILD_HELPERS=%SOURCE_PATH%%OPTION_FLUID_FOLDER_NAME%\build\build_helpers\
set BUILD_TYPE=1

:OPTIONS
rem - autorun mode if command line parameters are provided
CLS
IF /I "%1" == "/A" GOTO BUILD_ALL
IF /I "%1" == "/D" GOTO BUILD_DEBUG
IF /I "%1" == "/R" GOTO BUILD_RELEASE

rem - manual run mode if known command line parameters are not provided
echo ------------------
echo fluid - Build Tool
echo ------------------
echo     Root: %ROOT% maps to: %ABSOLUTE_PATH%
echo.
echo   Source: %SOURCE_PATH%
echo    Build: %BUILD_PATH%
echo.
echo  Options: %BUILD_OPTIONS%
echo.
echo.
echo ------------------
echo Select build type:
echo ------------------
echo 1. Build All [same as build /A]
echo 2. Debug Build (Primary) [same as build /D]
echo 3. Release Build (Primary) [same as build /R]
echo 4. Exit
CHOICE /C:1234
IF ERRORLEVEL == 4 GOTO END
IF ERRORLEVEL == 3 GOTO BUILD_RELEASE
IF ERRORLEVEL == 2 GOTO BUILD_DEBUG
IF ERRORLEVEL == 1 GOTO BUILD_ALL

:BUILD_ALL
rem - setup build type variables
set BUILD_TYPE=1
GOTO BUILD

:BUILD_DEBUG
rem - setup build type variables
set BUILD_TYPE=2
GOTO BUILD

:BUILD_RELEASE
rem - setup build type variables
set BUILD_TYPE=3
GOTO BUILD

:BUILD
rem - clear screen
CLS

:PRE_BUILD
rem - clean build path by deleting all files and folder
echo building...0%% 
echo. > "%BUILD_LOG%"
echo. >> "%BUILD_LOG%"
echo [[[Pre-build operations]]] >> "%BUILD_LOG%"
echo. >> "%SOURCE_PATH%build.log"
echo [Cleaning folder: "%BUILD_PATH%"] >> "%BUILD_LOG%"
DEL "%BUILD_PATH%"*.* /S /Q >> "%BUILD_LOG%"
RD "%BUILD_PATH%" /S /Q >> "%BUILD_LOG%"
MD "%BUILD_PATH%" >> "%BUILD_LOG%"

:ASSEMBLE_FLUID_JS
rem - assemble fluid.js if configured to build or if does not exists
IF %OPTION_REBUILD_FLUIDJS% EQU 1 CALL "%BUILD_HELPERS%assemble_fluid_js.bat"
IF %OPTION_REBUILD_FLUIDJS% EQU 0 IF NOT EXIST "%SOURCE_PATH%%OPTION_FLUID_FOLDER_NAME%\fluid.debug.js" CALL "%BUILD_HELPERS%assemble_fluid_js.bat"

rem - skip creating minified files if only DEBUG build
IF %BUILD_TYPE% NEQ 2 GOTO MINIFY
GOTO TEMP_BUILD

:MINIFY
rem - minify all .debug.* files into .min.g.*
rem - minify all .debug.js files into .min.g.js (ignoring those .debug.js files for which corrosponding .min.js already exists)
echo building...15%%
echo. >> "%BUILD_LOG%"
echo [Generating: source\(minified JS files)] >> "%BUILD_LOG%"
setlocal enabledelayedexpansion
FOR /R "%SOURCE_PATH%" %%G IN (*.debug.js) DO (
	SET debugFileName=%%G
	rem - replace .debug.js to .min.g.js
	SET minFileName=!debugFileName:.debug.js=.min.g.js!
	SET prebuiltMinFile=!debugFileName:.debug.js=.min.js!
	IF EXIST "!prebuiltMinFile!" echo Using !prebuiltMinFile! >> "%BUILD_LOG%"
	IF NOT EXIST "!prebuiltMinFile!" ( 
		echo !debugFileName! to !minFileName! >> "%BUILD_LOG%"
		IF EXIST "%AJAXMIN%" "%AJAXMIN%" "!debugFileName!" -out "!minFileName!" %AJAXMIN_OPTIONS_JS% >> "%BUILD_LOG%"
		IF NOT EXIST "%AJAXMIN%" COPY "!debugFileName!" "!minFileName!" /Y >> "%BUILD_LOG%"
	)
) 
echo building...17%%
rem - minify all .debug.css files into .min.g.css (ignoring those .debug.css files for which corrosponding .min.css already exists)
echo building...19%%
echo. >> "%BUILD_LOG%"
echo [Generating: source\(minified CSS files)] >> "%BUILD_LOG%"
setlocal enabledelayedexpansion
FOR /R "%SOURCE_PATH%" %%G IN (*.debug.css) DO (
	SET debugFileName=%%G
	rem - replace .debug.css to .min.g.css
	SET minFileName=!debugFileName:.debug.css=.min.g.css!
	SET prebuiltMinFile=!debugFileName:.debug.css=.min.css!
	IF EXIST "!prebuiltMinFile!" echo Using !prebuiltMinFile! >> "%BUILD_LOG%"
	IF NOT EXIST "!prebuiltMinFile!" ( 
		echo !debugFileName! to !minFileName! >> "%BUILD_LOG%"
		IF EXIST "%AJAXMIN%" "%AJAXMIN%" "!debugFileName!" -out "!minFileName!" %AJAXMIN_OPTIONS_CSS% >> "%BUILD_LOG%"
		IF NOT EXIST "%AJAXMIN%" COPY "!debugFileName!" "!minFileName!" /Y >> "%BUILD_LOG%"
	)
) 
echo building...21%%

rem - convert fluid.min.g.js to fluid.min.js, if required - just to keep last build available all the time
IF EXIST "%SOURCE_PATH%%OPTION_FLUID_FOLDER_NAME%\fluid.min.g.js" REN "%SOURCE_PATH%%OPTION_FLUID_FOLDER_NAME%\fluid.min.g.js" "fluid.min.js"

:TEMP_BUILD
rem - copy all source files to temp debug and release folders (as required)
CALL "%BUILD_HELPERS%temp_build.bat"

rem - create build of choice if DEBUG build 
IF %BUILD_TYPE% NEQ 2 GOTO BUILD_ALL_TYPES

:BUILD_PREFERRED_TYPE
rem - create preferred type build
IF %OPTION_PRIMARY_BUILD_TYPE% EQU 0 GOTO BUILD_ALL_TYPES
IF %OPTION_PRIMARY_BUILD_TYPE% EQU 1 CALL "%BUILD_HELPERS%portable_app.bat"
IF %OPTION_PRIMARY_BUILD_TYPE% EQU 2 CALL "%BUILD_HELPERS%online_app.bat"
IF %OPTION_PRIMARY_BUILD_TYPE% EQU 3 CALL "%BUILD_HELPERS%offline_app.bat"
IF %OPTION_PRIMARY_BUILD_TYPE% EQU 4 CALL "%BUILD_HELPERS%cws_hosted_app.bat"
IF %OPTION_PRIMARY_BUILD_TYPE% EQU 5 CALL "%BUILD_HELPERS%wrapped_app.bat"
IF %OPTION_PRIMARY_BUILD_TYPE% EQU 6 CALL "%BUILD_HELPERS%portable_server_app.bat"

rem - done building
GOTO POST_BUILD

:BUILD_ALL_TYPES
CALL "%BUILD_HELPERS%portable_app.bat"
CALL "%BUILD_HELPERS%online_app.bat"
CALL "%BUILD_HELPERS%offline_app.bat"
CALL "%BUILD_HELPERS%cws_hosted_app.bat"
CALL "%BUILD_HELPERS%wrapped_app.bat"
CALL "%BUILD_HELPERS%portable_server_app.bat"

:POST_BUILD
rem - delete temp debug and release folders (as required)
echo building...99%%
echo. >> "%BUILD_LOG%"
echo [[[Post-build cleanup]]] >> "%BUILD_LOG%"
echo. >> "%BUILD_LOG%"
DEL "%TEMP_ROOT_PATH%*.*" /S /Q >> "%BUILD_LOG%"
RD "%TEMP_ROOT_PATH%" /S /Q >> "%BUILD_LOG%"
echo. >> "%BUILD_LOG%"
echo [[[completed]]] >> "%BUILD_LOG%"
echo. >> "%BUILD_LOG%"
echo building...100%%

:COMPLETE
rem - open build path or main html if created
IF %OPTION_OPEN_AFTER_BUILD% EQU 0 GOTO END
IF %BUILD_TYPE% NEQ 2 explorer "%BUILD_PATH%"
IF %BUILD_TYPE% EQU 2 IF %OPTION_PRIMARY_BUILD_TYPE% EQU 0 explorer "%BUILD_PATH%"
IF %BUILD_TYPE% EQU 2 IF %OPTION_PRIMARY_BUILD_TYPE% EQU 1 start %OPTION_BROWSER% "%PORTABLE_APP_DEBUG_PATH%%OPTION_STARTUP_HTML_NAME%.html"
IF %BUILD_TYPE% EQU 2 IF %OPTION_PRIMARY_BUILD_TYPE% EQU 2 start %OPTION_BROWSER% "%ONLINE_APP_LOCAL_URL%"
IF %BUILD_TYPE% EQU 2 IF %OPTION_PRIMARY_BUILD_TYPE% EQU 3 start %OPTION_BROWSER% "%OFFLINE_APP_LOCAL_URL%"
IF %BUILD_TYPE% EQU 2 IF %OPTION_PRIMARY_BUILD_TYPE% EQU 4 start %OPTION_BROWSER% "%CWS_HOSTED_APP_LOCAL_URL%"
IF %BUILD_TYPE% EQU 2 IF %OPTION_PRIMARY_BUILD_TYPE% EQU 5 explorer "%BUILD_PATH%"
GOTO END

:SETUP_ENV
CLS

echo ------------------
echo fluid - Build Tool
echo ------------------
echo.
echo It seems you have not done required local environment setup as
echo defined in readme.txt.
echo.
echo You should run build.bat once you are done with required local
echo development environment setup.
echo.
echo ------------------

:END
rem - release all variables
ENDLOCAL

rem - exit
EXIT /B 0